/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.springframework.lang.Nullable;

/**
 * @author yhd
 * @since 2021-09-28 20:21:00
 * 接口中定义了对于资源的判断、对资源的获取、对资源描述的获取。通过该接口可以对资源进行有效的操作。但是Resource接口注重于对资源的读取。
 */
public interface Resource extends InputStreamSource {

	/*判断是否存在*/
	boolean exists();
	/*判断是否可读*/
	default boolean isReadable() {
		return exists();
	}
	/*判断是否可以重复读取，如果为true表示不可以重复读取，在读取完成之后，需要关闭流*/
	default boolean isOpen() {
		return false;
	}
	/*判断是否是文件*/
	default boolean isFile() {
		return false;
	}
	/*获取URL地址*/
	URL getURL() throws IOException;
	/*获取URL地址*/
	URI getURI() throws IOException;
	/*获取文件*/
	File getFile() throws IOException;
	/*默认通过输入流获取nio的只读字节流管道*/
	default ReadableByteChannel readableChannel() throws IOException {
		return Channels.newChannel(getInputStream());
	}
	/*资源长度*/
	long contentLength() throws IOException;
	/*上次更新时间*/
	long lastModified() throws IOException;
	/*根据资源的当前位置，获取相对位置的其他资源*/
	Resource createRelative(String relativePath) throws IOException;
	/*返回资源名称*/
	@Nullable
	String getFilename();
	/*返回资源描述*/
	String getDescription();

}
